<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/1/7
 * Time: 23:42
 */
namespace app\model;

use think\facade\Db;
use think\Model;

class Supplies extends Model
{

    private function _getListField()
    {
        return [
            's_id' => 'suppliesId',
            's_name' => 'name',
            's_type' => 'type',
            'st_name' => 'typeName',
            's_brand' => 'brand',
            's_spec' => 'spec',
            's_unit' => 'unit',
            's_status' => 'status',
            's_create_time' => 'createTime',
        ];
    }

    public function getSuppliesInfoById($id,$field = '')
    {
        $field = $field ?: self::_getListField();
        return Db::table('supplies')
            ->leftJoin('user','user.id=s_uid')
            ->leftJoin('supplies_type','s_type=st_id')
            ->leftJoin('organization','organization.id=s_orgid')
            ->where(['s_id'=>$id])->field($field)->find();
    }

    public function addOneSupplies($data)
    {
        $data['s_create_time'] = time();
        return Db::table('supplies')->insertGetId($data);
    }

    public function getSuppliesListByCondition($where = [],$order = 's_id desc',$field = '')
    {
        $field = $field ?: self::_getListField();
        $list = Db::table('supplies')
            ->leftJoin('supplies_type','s_type=st_id')
            ->leftJoin('user','user.id=s_uid')
            ->leftJoin('organization','organization.id=s_orgid')
            ->where($where)
            ->field($field)
            ->order($order)
            ->paginate(input('param.pageSize'))
            ->render();
        return $list;
    }

}